from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from  rest_framework import mixins
from  rest_framework import generics

from rest_framework.pagination import PageNumberPagination

from .models import Goods,GoodsCategory

from .serializers import GoodsSerializers, CategorySerialzers

from rest_framework import viewsets

from django_filters.rest_framework import DjangoFilterBackend

from rest_framework import filters

# from django_filters import rest_framework as filters

from .filters import GoodsFilter


from rest_framework.authentication import TokenAuthentication
# class GoodsListView(APIView):
#     """
#
#     """
#     def get(self,request,format=None):
#         goods = Goods.objects.all()[:10]
#         goods_serializer = GoodsSerializers(goods,many=True)
#         return Response(goods_serializer.data)

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    page_query_param = 'page'
    max_page_size = 1000

class GoodsListView(generics.ListAPIView):
    """
      商品列表页
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializers
    pagination_class = StandardResultsSetPagination


class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet, mixins.RetrieveModelMixin):
    """
        商品列表页, 分页， 搜索， 过滤， 排序
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializers
    pagination_class = StandardResultsSetPagination
    # authentication_classes = (TokenAuthentication,)

    # filters.SearchFilter,filters.OrderingFilter
    filter_backends = (DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter)
    # filter_fields = ('name','shop_price')
    # filterset_class = GoodsFilter
    filter_class = GoodsFilter
    search_fields = ('=name', 'goods_brief', 'goods_desc')
    #  排序 销量和价格
    ordering_fields = ('sold_num', 'shop_price')

class GoodsCategoryViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    """
      list:
        商品分类的数据
    """
    queryset = GoodsCategory.objects.filter(category_type=1)
    # GoodsCategory.objects.all()
    serializer_class = CategorySerialzers





